#include "mysig.h"


void (*signal2(int signo, void (*func)(int))) (int);
void pr_mask(const char*);
void err_sys(const char*);

static void sigint_handler(int);

int main() {
    sigset_t newmask, oldmask, waitmask;
    pr_mask("program start: ");
    if (signal2(SIGINT, sigint_handler) == SIG_ERR)
        err_sys("signal(SIGINT) error");
    sigemptyset(&waitmask);
    sigaddset(&waitmask, SIGUSR1);
    sigemptyset(&newmask);
    sigaddset(&newmask, SIGINT);
/*
 * Block SIGINT and save current signal mask.
 */
    if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
        err_sys("SIG_BLOCK error");
/*
 * Critical region of code.
 */
    pr_mask("in critical region: ");
/*
 * Pause, allowing all signals except SIGUSR1.
 */
    if (sigsuspend(&waitmask) != -1)
        err_sys("sigsuspend error");
    return 0;
}


void sigint_handler(int sig) {
    pr_mask("In sigint_handler: ");
}
